/* 深克隆 */
function deepClone(obj) {  //还可以利用JSON进行处理
    if (obj === null || typeof obj !== 'object') {
        return obj;
    }
    let clone = Array.isArray(obj) ? [] : {};
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
            clone[key] = deepClone(obj[key]);
        }
    }
    return clone;
}

/* 日期格式化 */
function formatTime(time,template){
    if(typeof time !== 'string'){
        time = new Date().toLocaleString('zh-CN',{ hour12:false })
    }
    if(typeof template !== 'string'){
        template = '{0}年{1}月{2}日 {3}:{4}:{5}'
    }
    let arr = []
    if(/^\d{8}$/.test(time)){
        let [,$1,$2,$3] = /^(\d{4})(\d{2})(\d{2})$/.exec(time)
        arr.push($1,$2,$3)
    }else{
        arr = time.match(/\d+/g)
    }
    return template.replace(/\{(\d+)\}/g,(_,$1)=>{
        //$1是匹配到的子串中的第一个捕获组（即第一个括号内的内容）
        let item = arr[$1] || '00'
        if(item.length < 2) item = '0' + item
        return item
    })
}

/* 设定具备有效期的localStorage存储方案 */
const storage = {
    set(key,value){
        localStorage.setItem(key,JSON.stringify({
            time: +new Date(),
            value
        }))
    },
    get(key,cycle=2592000000){ //cycle的默认值是30天
        cycle = +cycle
        if(isNaN(cycle)) cycle = 2592000000
        let data = localStorage.getItem(key)
        if(!data) return null
        let {time,value} = JSON.parse(data)
        if((+new Date() - time) > cycle){
            storage.remove(key)
            return null
        }
        return value
    },
    remove(key){
        localStorage.removeItem(key)
    }
}

export default {
    deepClone,
    formatTime,
    storage
}